﻿/*
 * Jsofteditor - The text editor for Internet - http://www.jsofteditor.net
 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * JsoftPastePlainTextCommand Class: represents the
 * "Paste as Plain Text" command.
 */

var JsoftTableCommand = function( command )
{
	this.Name = command ;
}

JsoftTableCommand.prototype.Execute = function()
{
	JsoftUndo.SaveUndoStep() ;

	if ( ! JsoftBrowserInfo.IsGecko )
	{
		switch ( this.Name )
		{
			case 'TableMergeRight' :
				return JsoftTableHandler.MergeRight() ;
			case 'TableMergeDown' :
				return JsoftTableHandler.MergeDown() ;
		}
	}

	switch ( this.Name )
	{
		case 'TableInsertRowAfter' :
			return JsoftTableHandler.InsertRow( false ) ;
		case 'TableInsertRowBefore' :
			return JsoftTableHandler.InsertRow( true ) ;
		case 'TableDeleteRows' :
			return JsoftTableHandler.DeleteRows() ;
		case 'TableInsertColumnAfter' :
			return JsoftTableHandler.InsertColumn( false ) ;
		case 'TableInsertColumnBefore' :
			return JsoftTableHandler.InsertColumn( true ) ;
		case 'TableDeleteColumns' :
			return JsoftTableHandler.DeleteColumns() ;
		case 'TableInsertCellAfter' :
			return JsoftTableHandler.InsertCell( null, false ) ;
		case 'TableInsertCellBefore' :
			return JsoftTableHandler.InsertCell( null, true ) ;
		case 'TableDeleteCells' :
			return JsoftTableHandler.DeleteCells() ;
		case 'TableMergeCells' :
			return JsoftTableHandler.MergeCells() ;
		case 'TableHorizontalSplitCell' :
			return JsoftTableHandler.HorizontalSplitCell() ;
		case 'TableVerticalSplitCell' :
			return JsoftTableHandler.VerticalSplitCell() ;
		case 'TableDelete' :
			return JsoftTableHandler.DeleteTable() ;
		default :
			return alert( JsoftLang.UnknownCommand.replace( /%1/g, this.Name ) ) ;
	}
}

JsoftTableCommand.prototype.GetState = function()
{
	if ( Jsoft.EditorDocument != null && JsoftSelection.HasAncestorNode( 'TABLE' ) )
	{
		switch ( this.Name )
		{
			case 'TableHorizontalSplitCell' :
			case 'TableVerticalSplitCell' :
				if ( JsoftTableHandler.GetSelectedCells().length == 1 )
					return Jsoft_TRISTATE_OFF ;
				else
					return Jsoft_TRISTATE_DISABLED ;
			case 'TableMergeCells' :
				if ( JsoftTableHandler.CheckIsSelectionRectangular()
						&& JsoftTableHandler.GetSelectedCells().length > 1 )
					return Jsoft_TRISTATE_OFF ;
				else
					return Jsoft_TRISTATE_DISABLED ;
			case 'TableMergeRight' :
				return JsoftTableHandler.GetMergeRightTarget() ? Jsoft_TRISTATE_OFF : Jsoft_TRISTATE_DISABLED ;
			case 'TableMergeDown' :
				return JsoftTableHandler.GetMergeDownTarget() ? Jsoft_TRISTATE_OFF : Jsoft_TRISTATE_DISABLED ;
			default :
				return Jsoft_TRISTATE_OFF ;
		}
	}
	else
		return Jsoft_TRISTATE_DISABLED;
}
